import ustruct

class MPU6050:
    def __init__(self, i2c, addr=0x68):
        self.i2c = i2c
        self.addr = addr
        self.init()
        print('mpu6050 inited!')

    def init(self):
        self.i2c.start()
        self.i2c.writeto(self.addr, bytearray([107, 0]))
        self.i2c.stop()
    
    def read_accelerometer(self):
        self.i2c.start()
        raw = self.i2c.readfrom_mem(self.addr, 0x3b, 6)
        self.i2c.stop()
        accel = ustruct.unpack(">hhh", raw)
        return accel

    def read_gyroscope(self):
        self.i2c.start()
        raw = self.i2c.readfrom_mem(self.addr, 0x43, 6)
        self.i2c.stop()
        gyro = ustruct.unpack(">hhh", raw)
        return gyro

    def read_temperature(self):
        self.i2c.start()
        raw = self.i2c.readfrom_mem(self.addr, 0x41, 2)
        self.i2c.stop()
        temp = ustruct.unpack(">h", raw)[0] / 340.00 + 36.53
        return temp